<template>
    <section class="form-container">
        <el-form :inline="true" :model="filter" ref="filterRef" size="mini" class="form-filter">
            <el-form-item label="总线路组名称" prop="lineGroupTotalName">
                <el-input v-model="filter.lineGroupTotalName" placeholder="请输入条件查询" @change="getPage()"/>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="getPage()" icon="el-icon-search">查询</el-button>
                <el-button @click="resetFilter()" icon="el-icon-refresh-left">重置</el-button>
                <el-button @click="showUpdate({})" icon="el-icon-plus">添加</el-button>
            </el-form-item>
        </el-form>
        <div class="form-content">
            <el-table
                v-loading="loading"
                style="width: 100%"
                :data="page.records"
                border
                stripe
                :cell-class-name="getCellIndex"
                @cell-click="tableDbEdit"
                :header-cell-style = "headerCellStyle"
            >
                <el-table-column label="序号" width="60">
                    <template #default="scope">
                        {{ scope.$index + (page.current - 1) * page.size + 1 }}
                    </template>
                </el-table-column>
                <el-table-column prop="lineGroupTotalName" label="总线路组名称"/>
                <el-table-column prop="lineGroupTotalSpell" label="总线路组简拼"/>
                <el-table-column prop="remark" label="备注"/>
                <el-table-column property="sort" sortable label="排序">
                    <template #default="scope">
                    <span v-show="!(scope.row.index == coordinate.row && scope.column.index ==coordinate.column)">
                    {{ scope.row.sort }}
                    </span>

                        <el-input
                            v-show="scope.row.index == coordinate.row && scope.column.index ==coordinate.column"
                            ref="sort"
                            v-model="scope.row.sort"
                            size="mini"
                            autofocus
                            @change="submitTableEdit(scope.row)"
                        />
                    </template>
                </el-table-column>
                <el-table-column label="状态">
                    <template #default="scope">
                        <el-switch
                            v-model="scope.row.status"
                            @change="statusSwitch(scope.row.status,scope.row.id)"
                        />
                    </template>
                </el-table-column>
                <el-table-column prop="updateName" label="更新人"/>
                <el-table-column prop="updateTime" label="更新时间"/>
                <el-table-column fixed="right" label="操作" width="150">
                    <template #default="scope">
                        <el-button size="mini" @click="showUpdate(scope.row)">编辑
                        </el-button>
                        <el-popconfirm title="确定删除吗？" @onConfirm="handleDelete(scope.row.id)">
                            <el-button slot="reference" size="mini" style="margin-left:10px;">删除
                            </el-button>
                        </el-popconfirm>
                    </template>
                </el-table-column>
            </el-table>

            <el-pagination
                background
                :current-page="page.current"
                :page-sizes="[10, 20, 30, 50,100]"
                :page-size="page.size"
                layout="total, sizes, prev, pager, next, jumper"
                :total="page.total"
                style="margin-top:20px;"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
            />

            <Update ref="updateRef" @refresh="getPage()"/>
        </div>
    </section>
</template>
<script>
import {LineGroupTotalApi} from '@/api/intercity/LineGroupTotalApi'
import Update from './Update.vue'

export default {
    name: 'lineGroupTotal',
    components: {
        Update
    },
    data() {
        return {
            page: {
                total: 0,
                size: 10,
                current: 1,
                pages: 0,
                records: []
            },
            filter: {lineGroupTotalName: ''},
            coordinate: {column: -1, row: -1},
            loading: false,
            headerCellStyle:{
                "background-color":"#F2F5FC"
            }
        }
    },
    created() {
        this.getPage()
    },
    methods: {
        // 获取用户列表
        getPage() {
            const params = {
                pageIndex: this.page.current,
                pageSize: this.page.size,
                lineGroupTotalName: this.filter.lineGroupTotalName
            }

            this.loading = true
            LineGroupTotalApi.getPage(params).then((response) => {
                this.page = response
                this.loading = false
            }).catch((err) => {
                this.loading = false
            })
        },

        // 修改状态
        statusSwitch(val, id) {
            const params = {
                id: id,
                status: val
            }

            LineGroupTotalApi.updateStatus(params).then((response) => {
            })
        },

        tableDbEdit(row, column) {
            this.coordinate.row = row.index
            this.coordinate.column = column.index
        },

        // 修改排序
        submitTableEdit(row) {
            this.coordinate.column = -1
            this.coordinate.row = -1
            const params = {
                id: row.id,
                sort: row.sort
            }
            LineGroupTotalApi.updateSort(params).then((response) => {
            })
        },

        // 删除数据
        handleDelete(id) {
            LineGroupTotalApi.delete(id).then((response) => {
                this.getPage()
            })
        },

        handleSizeChange(val) {
            this.page.size = val
            this.page.current = 1
            this.getPage()
        },

        handleCurrentChange(val) {
            this.page.current = val
            this.getPage()
        },

        getCellIndex({row, column, rowIndex, columnIndex}) {
            row.index = rowIndex
            column.index = columnIndex
        },

        showUpdate(data) {
            this.$refs['updateRef'].showUpdate(data)
        },

        resetFilter() {
            this.$refs['filterRef'].resetFields()
            this.getPage()
        }
    }
}

</script>

<style lang="scss" scoped></style>
